package yl.hs.bmipfull.controllers.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IAppSettingService;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.Map;

/*
 * AppSettingController 系统设置信息控制器
 * @category 系统管理——系统设置
 * @author 谢景广
 * @date 2021-05-13
 */
@Api(value = "系统设置信息Web接口", tags = "系统管理——系统设置")
@RequestMapping(path = "/api/system/setting")
@RestController
public class AppSettingController extends ControllerBase {
    @Autowired
    IAppSettingService appSetSvc;

    @ApiOperation(value = "查询系统设置", notes = "查询系统设置")
    @RequestMapping(path = "{unitSN}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult queryAll(@PathVariable("unitSN") String unitSN) {
        var data = appSetSvc.queryAllUnitSn(unitSN.split(","));
        return success("ok", data);
    }

    @ApiOperation(value = "创建新的系统设置项", notes = "创建新的系统设置项")
    @RequestMapping(path = "", method = RequestMethod.POST)
    @ResponseBody
    public WebResult creatParam(@RequestBody SubmitModel model) {
        try {
            var data = appSetSvc.saveCreateAppSet(model);
            if (data > 0) {
                return success("ok");
            }
            return fail("数据保存失败");
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "保存系统设置", notes = "保存系统设置")
    @RequestMapping(path = "", method = RequestMethod.PUT)
    @ResponseBody
    public WebResult updateParam(@RequestBody Map<String, Object> model) {
        try {

            var data = appSetSvc.saveUpdateAppSets(model);
            if (data > 0) {
                return success("ok");
            }
            return success("数据已保存");
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询系统设置", notes = "查询系统设置")
    @RequestMapping(path = "{unitSN}/{code}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult checkCodeExists(@PathVariable("unitSN") String unitSN, @PathVariable("code") String code) {
        var data = appSetSvc.queryAllUnitSn(unitSN.split(","));
        return success("ok", data);
    }


}
